package com.atguigu.campusintelligentassistantsystem.service;

import com.atguigu.campusintelligentassistantsystem.entity.LibraryBook;
import com.atguigu.campusintelligentassistantsystem.entity.StudentBook;
import com.atguigu.campusintelligentassistantsystem.repository.LibraryBookRepository;
import com.atguigu.campusintelligentassistantsystem.repository.StudentBookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class LibraryService {

    @Autowired
    private LibraryBookRepository libraryBookRepository;

    @Autowired
    private StudentBookRepository studentBookRepository;

    /**
     * 搜索图书
     * @param keyword 关键词
     * @return 图书列表
     */
    public List<LibraryBook> searchBooks(String keyword) {
        // 根据书名、作者或ISBN搜索
        List<LibraryBook> booksByName = libraryBookRepository.findByBookNameContaining(keyword);
        List<LibraryBook> booksByAuthor = libraryBookRepository.findByAuthorContaining(keyword);
        
        // 合并结果并去重
        booksByName.addAll(booksByAuthor);
        return booksByName.stream().distinct().toList();
    }

    /**
     * 根据ISBN查找图书
     * @param isbn ISBN
     * @return 图书列表
     */
    public List<LibraryBook> findBooksByIsbn(String isbn) {
        return libraryBookRepository.findByIsbn(isbn);
    }

    /**
     * 获取所有图书
     * @return 图书列表
     */
    public List<LibraryBook> getAllBooks() {
        return libraryBookRepository.findAll();
    }

    /**
     * 根据ID获取图书
     * @param id 图书ID
     * @return 图书信息
     */
    public LibraryBook getBookById(Long id) {
        Optional<LibraryBook> book = libraryBookRepository.findById(id);
        return book.orElse(null);
    }

    /**
     * 添加新图书
     * @param book 图书信息
     * @return 保存后的图书
     */
    public LibraryBook addBook(LibraryBook book) {
        return libraryBookRepository.save(book);
    }

    /**
     * 更新图书信息
     * @param book 图书信息
     * @return 更新后的图书
     */
    public LibraryBook updateBook(LibraryBook book) {
        return libraryBookRepository.save(book);
    }

    /**
     * 删除图书
     * @param id 图书ID
     */
    public void deleteBook(Long id) {
        libraryBookRepository.deleteById(id);
    }

    /**
     * 学生借阅图书
     * @param studentId 学生ID
     * @param bookId 图书ID
     * @return 借阅记录
     */
    public StudentBook borrowBook(Long studentId, Long bookId) {
        Optional<LibraryBook> bookOpt = libraryBookRepository.findById(bookId);
        if (bookOpt.isPresent() && bookOpt.get().getStock() > 0) {
            // 更新图书库存
            LibraryBook book = bookOpt.get();
            book.setStock(book.getStock() - 1);
            libraryBookRepository.save(book);

            // 创建借阅记录
            StudentBook studentBook = new StudentBook();
            studentBook.setStudentId(studentId);
            studentBook.setBookId(bookId);
            studentBook.setBorrowTime(LocalDateTime.now());
            // 设置归还时间为30天后
            studentBook.setReturnTime(LocalDateTime.now().plusDays(30));
            return studentBookRepository.save(studentBook);
        }
        return null;
    }

    /**
     * 续借图书
     * @param id 借阅记录ID
     * @return 更新后的借阅记录
     */
    public StudentBook renewBook(Long id) {
        Optional<StudentBook> studentBookOpt = studentBookRepository.findById(id);
        if (studentBookOpt.isPresent()) {
            StudentBook studentBook = studentBookOpt.get();
            // 延长归还时间15天
            studentBook.setReturnTime(studentBook.getReturnTime().plusDays(15));
            return studentBookRepository.save(studentBook);
        }
        return null;
    }

    /**
     * 获取学生的借阅记录
     * @param studentId 学生ID
     * @return 借阅记录列表
     */
    public List<StudentBook> getStudentBooks(Long studentId) {
        return studentBookRepository.findByStudentId(studentId);
    }

    /**
     * 获取图书的借阅记录
     * @param bookId 图书ID
     * @return 借阅记录列表
     */
    public List<StudentBook> getBookBorrowRecords(Long bookId) {
        return studentBookRepository.findByBookId(bookId);
    }
}